package com.globallogic.token;

import java.util.Iterator;
import java.util.List;

import com.globallogic.draw.DrawContainer;
import com.globallogic.draw.Drawable;
import com.globallogic.inout.Out;

public class TokenHandlerPtInside implements TokenHandler {
		
	@Override
	public void execute(TokenProcessor tokenProcessor, List<String> params) {

		Iterator<Drawable> iterator = DrawContainer.getInstance().iterator();
		Drawable drawObject;
		int cnt = 0;		
		
		if (!parceParams(params)) {
			
			return;
			
		}
		
    	while (iterator.hasNext()) {
    			
    		drawObject = iterator.next();
    		Out.printLn("Point (" + x + ", " + y + ") is inside " + drawObject + ": " + drawObject.isPointInside(x, y));
    		cnt++;
    			
    	}
		    
    	if (cnt == 0) {
    		
    		Out.printLn("No object has been added!");
        	
        }

	}
	
	private boolean parceParams(List<String> args) {

		if (args.size() < 2) {
            
			Out.printLn("Not enought parameters for ptinside command");
            return false;
            
		} 			
		
		try {

			x = Integer.parseInt(args.get(0));
			y = Integer.parseInt(args.get(1));
			
		    if ((x < 0) || (y < 0)) {
		    	
		    	Out.printLn("x and y  should be positive integer values");
                return false;
				
		    }
			
		    return true;
		    
		} catch (NumberFormatException e) {
			
			Out.printLn("x and y should be integer values");
			
		}
		
		return false;
		
	}
	
	private int x;
	private int y;
	
}
